package com.wisezone.demo0217;


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) {

        Person p1 = new Person();

        Person p2 = new Person();
        if (p1.getClass()==p2.getClass()){
            System.out.println("相同。");
        }

//        Person person = new Person();
//        person.setName("张三");
//        person.setAge(20);
//        person.show();
//        Class<?>clzz
          Class clzz = Person.class;
        try {
//            Person person =(Person) clzz.newInstance();
            Constructor<Person>[] cs = clzz.getConstructors();
            Constructor<Person>constructor = clzz.getConstructor(String.class,int.class);
            Person person = constructor.newInstance("张三",30);

//            person.setAge(20);
//            person.setName("张三");

            Field filed = clzz.getDeclaredField("name");
            filed.setAccessible(true);//针对私有的变量授权
            filed.set(person,"张三");

//            person.show();
            Method method = clzz.getMethod("show");
//            method.setAccessible(true);//针对私有的方法授权
            method.invoke(person);

            Method m2 = clzz.getMethod("display",String.class);
            m2.invoke(person,"南京");
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }catch (NoSuchFieldException e){
            e.printStackTrace();
        }catch (NoSuchMethodException e){
            e.printStackTrace();
        }catch (InvocationTargetException e){
            e.printStackTrace();
        }
    }
}
